// pages/modifyUserInfo/detailUserInfo/detailUserInfo.js
var app = getApp();
import Dialog from '@vant/weapp/dialog/dialog'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        showCutImage: false,
        image_url: "",

    },

    returnImageUrl(e){
        this.setData({
            showCutImage: false
        })
        this.upload_file(e.detail.imageUrl);
        // console.log(e);
    },
    uploadHeadImg(){
        var a = this;
        wx.showActionSheet({
          itemList: ["从相册中选择", "拍照"],
          itemColor: "#f7982a",
          success:function(e){
              e.cancel || (0 == e.tapIndex ? a.chooseWxImageShop("album") : 1 == e.tapIndex && a.chooseWxImageShop("camera"));
          }
        })
    },

    chooseWxImageShop(a){
        var e = this;
        wx.chooseImage({
          sizeType: 'compressed',
          count: 1,
          sourceType: [a],
          success: function(a){
              if (a.tempFiles[0].size > 5097152){
                  wx.showModal({
                    title: "提示",
                    cancelColor: '',
                    content: "选择的图片过大， 请不要上传超过2M的图片",
                    showCancel: !1,
                    success: function(a){
                        a.confirm;
                    }
                  })
              }else{
                  e.setData({
                      image_url: a.tempFilePaths[0],
                      showCutImage: true
                  })
              }
          }
        })
    },

    upload_file(e){
        var _this = this;
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          filePath: e,
          name: 'file',
          url: 'https://lzpi1.grass.wang/image/uploadImage?userAccount=18530794371',
          header: {
              "Content-Type": "multipart/form-data"
          },
          success: (a) => {
              const res = JSON.parse(a.data);
              console.log(res);
              if (res.code === 200){
                wx.hideLoading();
                console.log(res.data);
                _this.modifyAvatar(res.data);
              }else{
                wx.hideLoading();
                wx.showToast({
                    title: '上传失败',
                    icon: "error",
                    duration: 3000
                });
              }
              

          },
          fail:function(a){
            wx.hideLoading();
            wx.showToast({
                title: '上传失败',
                icon: "error",
                duration: 3000
            });
          }
        })
    },
    modifyAvatar(avatarUrl){
        wx.request({
        url:app.globalData.URL_PRE + '/user/modify/userAvatar',
        method: 'POST',
        data: JSON.stringify({"userAvatar": avatarUrl}),
        header: {
            "cookie": wx.getStorageSync('cookie')
        },
        success: (res)=>{
            //   console.log(res);
            app.globalData.userInfo.userAvatar=res.data.data;
            this.setData({
                ['userInfo.userAvatar']: res.data.data,
            })
            wx.showToast({
                title: '上传成功',
                icon: "success",
                duration: 3000
            });
        }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
          title: '个人资料',
        })
        this.setData({
            userInfo: app.globalData.userInfo
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            userInfo: app.globalData.userInfo
        })
    
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})